import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class OverviewPanel extends JPanel {
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem screenshot, about, exit;
	private EventHandler eventHandler;
	private TabHandler tabHandler;
	private JFrame aboutPopUp;
	private JLabel made, naam1, naam2, klas;
	private JTabbedPane weerTab;
	private int maand, jaar;
	private GrafiekView grafiekView;
	private MaandView maandView;

	public OverviewPanel() {
		grafiekView = new GrafiekView();
		maandView = new MaandView();
		Font font = new Font("SansSerif", Font.BOLD, 14);
		weerTab = new JTabbedPane();
		weerTab.addTab("Maand Overzicht", null, maandView,
				"Maand overzicht van Twente");
		weerTab.addTab("Grafiek Maand Overzicht", null, grafiekView, "grafiek");
		weerTab.setBackground(Color.LIGHT_GRAY);
		weerTab.setForeground(Color.RED);
		weerTab.setFont(font);

		menuBar = new JMenuBar();
		menu = new JMenu("menu");
		menuBar.add(menu);
		createPopUp();

		eventHandler = new EventHandler();
		tabHandler = new TabHandler();
		screenshot = new JMenuItem("Screenshot");
		exit = new JMenuItem("Exit");
		screenshot.addActionListener(eventHandler);
		exit.addActionListener(eventHandler);
		about = new JMenuItem("About");
		about.addActionListener(eventHandler);

		weerTab.addChangeListener(tabHandler);
		
		menu.add(screenshot);
		menu.add(about);
		menu.add(exit);

		menuBar.setBorderPainted(false);
		menuBar.setBackground(Color.LIGHT_GRAY);
		
		setLayout(new BorderLayout());
		add(weerTab, BorderLayout.CENTER);
		add(menuBar, BorderLayout.NORTH);
	}

	private void createPopUp() {
		made = new JLabel("  Made by:");
		naam1 = new JLabel("  Harm Koster");
		naam2 = new JLabel("  Ken Veenstra");
		klas = new JLabel("  ein2b, jan 2011");
		aboutPopUp = new JFrame();
		aboutPopUp.setVisible(false);
		aboutPopUp.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		aboutPopUp.setBounds(300, 200, 120, 120);
		aboutPopUp.setResizable(false);
		aboutPopUp.setLayout(new GridLayout(4, 1));
		aboutPopUp.add(made);
		aboutPopUp.add(naam1);
		aboutPopUp.add(naam2);
		aboutPopUp.add(klas);
	}

	class TabHandler implements ChangeListener {

		@Override
		public void stateChanged(ChangeEvent e) {
			System.out.println("asdasd");
				if (weerTab.getSelectedComponent() == grafiekView) {
					System.out.println("grafiek");
					maand = maandView.getMaand();
					jaar = maandView.getJaar();
					grafiekView.Sync(maand, jaar);
				} else {
					maand = grafiekView.getMaand();
					jaar = grafiekView.getJaar();
					maandView.Sync(maand, jaar);
				}
				revalidate();
				repaint();
		}
	}
		
	
	class EventHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
		 if(e.getSource() == (screenshot)) {
				try {
					saveScreenshot();
				} catch (AWTException e1) {
					e1.printStackTrace();
				}
			}
			if (e.getSource() == (exit)) {
				System.exit(0);
			}
			if (e.getSource() == (about)) {
				aboutPopUp.setVisible(true);
			}
		}

		public void saveScreenshot() throws AWTException {
			Robot robot;
			robot = new Robot();
			BufferedImage bi = robot.createScreenCapture(new Rectangle(weerTab
					.getLocationOnScreen().x,
					weerTab.getLocationOnScreen().y + 60, weerTab.getWidth(),
					weerTab.getHeight() - 60));

			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showSaveDialog(OverviewPanel.this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				try {
					ImageIO.write(bi, "jpg", new File(file.getPath() + ".jpg"));

				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}
